<template>
    <div>
        <!-- 商品分类导航 -->
        <div class="type-nav">
            <div class="container">
                <h2 class="all">全部商品分类</h2>

                <!--侧边导航 -->


                <div class="sort">
                    <ul class="all-sort-list2" id="js_climit_li">
                        <!-- 一级分类 -->
                        <!--一级分类地盘-->
                        <div class="item" v-for="(c1, index) in categoryList" :key="c1.categoryId">

                            <h3 @mouseenter="enterHandler(index)" :class="{ active: currentIndex == index }">
                                <a>{{
                                    c1.categoryName
                                }}</a>
                            </h3>
                            <!-- 通过JS实现动态行内样式，进行二级、三级分类的显示与隐藏(display:none|block切换的) -->
                            <div class="item-list clearfix" :style="{ display: currentIndex == index ? 'block' : 'none' }">
                                <!--二级分类-->
                                <div class="subitem" v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                                    <dl class="fore">
                                        <dt>
                                            <a>{{
                                                c2.categoryName
                                            }}</a>
                                        </dt>
                                        <dd>
                                            <!--三级分类-->
                                            <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                                                <a @click="gotoProductList(c3.categoryId)">{{ c3.categoryName
                                                }}</a>
                                            </em>
                                        </dd>
                                    </dl>
                                </div>
                            </div>
                        </div>

                    </ul>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
import axios from 'axios'
import throttle from 'lodash/throttle'
export default {
    name: 'typeNav',
    data() {
        return {
            categoryList: [],
            currentIndex: -1,
        }
    },
    mounted() {
        this.getCategoryList()

    },
    methods: {
        getCategoryList() {
            axios({
                headers: {
                    'token': localStorage.getItem('token')
                },
                method: "get",
                url: "http://localhost:8085/category/getCategoryList",
                data: {}
            }).then(res => {
                console.log(res)
                //1. 获取分类的数据
                this.categoryList = res.data.data.data
            })
        },
        //点击分类跳转到商品列表页面

        gotoProductList(cId) {
            //将数据存到localStorage里面
            //localStorage:整个浏览器的缓存，关闭浏览器还会存在，会永久存在，可以手动删除
            localStorage.setItem("cId", cId);
            //跳转到product_list.html
            this.$router.push('/product')
        },
        //鼠标进入的方法
        enterHandler: throttle(function (index) {
            //修改响应式数据
            this.currentIndex = index;
            //鼠标进入事件,假如用户的行为过快,会导致项目业务丢失【里面业务有很多，可能出现卡顿现象】。
            //一句话：用户行为过快,浏览器反应不过来,导致业务丢失!!!!
            //函数的防抖与节流技术
            console.log("处理业务" + index);
        }, 10),
        //鼠标移出事件
        leaveHandler() {
            //鼠标移出高亮的效果消失
            this.currentIndex = -1;
            //隐藏商品分类
            //鼠标离开:在search路由下,在修改数据
            if (this.$route.path != "/home") {
                this.show = false;
            }
        },
    }
}
</script>
<style></style>
<style scoped lang="less">
.type-nav {
    border-bottom: 2px solid #e1251b;

    .container {
        width: 1200px;
        margin: 0 auto;
        display: flex;
        position: relative;

        .all {
            width: 210px;
            height: 45px;
            background-color: #e1251b;
            line-height: 45px;
            text-align: center;
            color: #fff;
            font-size: 14px;
            font-weight: bold;
        }

        .nav {
            a {
                height: 45px;
                margin: 0 22px;
                line-height: 45px;
                font-size: 16px;
                color: #333;
            }
        }

        .sort {
            position: absolute;
            left: 0;
            top: 45px;
            width: 210px;
            height: 461px;
            position: absolute;
            background: #fafafa;
            z-index: 999;

            .all-sort-list2 {
                .item {
                    h3 {
                        line-height: 30px;
                        font-size: 16px;
                        text-align: center;
                        font-weight: 400;
                        overflow: hidden;
                        padding: 0 20px;
                        margin: 0;

                        a {
                            color: #333;
                        }

                        &:hover {
                            background-color: #ada8a8;
                        }
                    }

                    .item-list {
                        display: none;
                        position: absolute;
                        width: 734px;
                        min-height: 460px;
                        background: #f7f7f7;
                        left: 210px;
                        border: 1px solid #ddd;
                        top: 0;
                        z-index: 9999 !important;

                        .subitem {
                            float: left;
                            width: 650px;
                            padding: 0 4px 0 8px;

                            dl {
                                border-top: 1px solid #eee;
                                padding: 6px 0;
                                overflow: hidden;
                                zoom: 1;

                                &.fore {
                                    border-top: 0;
                                }

                                dt {
                                    float: left;
                                    width: 54px;
                                    line-height: 22px;
                                    text-align: right;
                                    padding: 3px 6px 0 0;
                                    font-weight: 700;
                                }

                                dd {
                                    float: left;
                                    width: 415px;
                                    padding: 3px 0 0;
                                    overflow: hidden;

                                    em {
                                        float: left;
                                        height: 14px;
                                        line-height: 14px;
                                        padding: 0 8px;
                                        margin-top: 5px;
                                        border-left: 1px solid #ccc;

                                        &:hover {
                                            background-color: #ada8a8;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    &:hover {
                        .item-list {
                            display: block;

                        }
                    }
                }
            }
        }
    }
}
</style>
